var carModule = (function (Vue) {

  var vm = new Vue({
    el: '#car',
    data: {
      lists: [],
      checkedAll: false,
      checkedAllText: '全选'
    },
    mounted: function () {
      this.cartView();
    },
    filters: {
      formatMoney: function (value) {
        return '￥' + value.toFixed(2);
      }
    },
    computed: {
      totalPrice () {
        var _this = this,
            money = 0;
        this.lists.forEach( function(item) {
           if (item.checked === true) {
              money += item.productQuantity * item.productPrice;
           }
         }); 
        return money
      }
    },
    methods: {
      cartView () {
        var _this = this;
        this.$http.get('./data/cartData.json').then(function (res) {
          _this.lists = res.body.result.list;
        })
      },
      add (list) {
        list.productQuantity++;
      },
      minus (list) {
        if (list.productQuantity === 1) {
          return ;
        }
        list.productQuantity--;
      },
      selectedProduct (list) {
        if(typeof list.checked === 'undefined') {
          this.$set(list, "checked", false)
        }else {
          list.checked = !list.checked;
        }
      },
      selectAll () {
        var _this = this;
        this.checkedAll = !this.checkedAll;
        this.lists.forEach( function(item) {
          if (typeof item.checked === 'undefined') {
            _this.$set(item, "checked", false)
          }
          if (_this.checkedAll == true) {
            item.checked = true;
            _this.checkedAllText = '全不选';
          }else {
            item.checked = false;
            _this.checkedAllText = '全选';
          }
        });
      },
      del (item) {
        var info = window.confirm('确认删除？');
        if (info) {
          var index = this.lists.indexOf(item);
          this.lists.splice(index, 1);
        }
      }
    }
  })
})(Vue);